<template>
    <div class="hospital-info">

        <el-card class="box-card">
            <div slot="header" class="clearfix">
                <div class="title">
                    <span>医院信息</span>
                </div>
                <div>

                </div>
            </div>
            <div class="text item">
                <el-form ref="form" :model="hospitalInfo" label-width="100px">
                    <el-form-item label="医院名称">
                        <el-input v-model="hospitalInfo.name" :disabled="true"></el-input>
                    </el-form-item>
                    <el-form-item label="医院简介">
                        <el-input v-model="hospitalInfo.introduce"></el-input>
                    </el-form-item>
                    <el-form-item label="医院地址">
                        <el-input v-model="hospitalInfo.address"></el-input>
                    </el-form-item>
                    <el-form-item label="加入时间">
                        <el-input v-model="hospitalInfo.createTime" :disabled="true"></el-input>
                    </el-form-item>
                    <el-form-item class="center">
                        <el-button type="primary" @click="updateHospital">保存</el-button>
                    </el-form-item>
                </el-form>
            </div>
        </el-card>

        <el-card class="box-card">
            <div slot="header" class="clearfix">
                <div class="title">
                    <span>负责人信息</span>
                </div>
                <div>

                </div>
            </div>
            <div class="text item">
                <el-form ref="form" :model="hospitalManagerInfo" label-width="100px">
                    <el-form-item label="头像">
                        <el-upload :http-request="uploadImage" action="" :show-file-list="false" :limit="1"
                            :on-exceed="handleExceed" ref="userAvatar">
                            <img :src="hospitalManagerInfo.avatar" alt="点击修改图片" width="60px">
                        </el-upload>
                    </el-form-item>
                    <el-form-item label="姓名">
                        <el-input v-model="hospitalManagerInfo.name"></el-input>
                    </el-form-item>
                    <el-form-item label="简介">
                        <el-input v-model="hospitalManagerInfo.introduce"></el-input>
                    </el-form-item>
                    <el-form-item label="邮箱">
                        <el-input v-model="hospitalManagerInfo.email"></el-input>
                    </el-form-item>
                    <el-form-item label="联系方式">
                        <el-input v-model="hospitalManagerInfo.phone"></el-input>
                    </el-form-item>
                    <el-form-item class="center">
                        <el-button type="primary" @click="updateHospitalAdmin">保存</el-button>
                    </el-form-item>
                </el-form>
            </div>
        </el-card>
    </div>
</template>

<script>
export default {
    name: "Information",
    data() {
        return {
        }
    },
    mounted() {
        this.getHostpitalInfo()
    },
    methods: {
        // 更新负责人信息
        async updateHospitalAdmin() {
            let res = await this.$store.dispatch("uploadAdminInfo", this.$store.state.hospital.hospitalManagerInfo);
            if (res.success) this.$message.success(res.message)
            else this.$myfun.resError(res)
        },
        // 更新医院信息
        async updateHospital() {
            let res = await this.$store.dispatch("uploadHospitalInfo", this.$store.state.hospital.hospitalInfo);
            if (res.success) this.$message.success(res.message)
            else this.$myfun.resError(res)
        },
        // 上传头像
        async uploadImage(param) {
            const formData = new FormData()
            formData.append('multipartFile', param.file)
            let res = await this.$store.dispatch("uploadAdminAvatar", formData);
            if (res.success) {
                this.$message.success(res.message)
                this.$store.commit("setUserAvatar", res.data)
                this.getHostpitalInfo()
            }
            else this.$myfun.resError(res)
        },
        // 超出限制
        handleExceed(files, fileList) {
            this.$message.warning('只能选择一张图片')
        },
        // 获取医院信息
        async getHostpitalInfo() {
            let res = await this.$store.dispatch("getHostpitalInfo")
            if (!res.success) this.$myfun.resError(res)
        }
    },
    computed: {
        hospitalManagerInfo() {
            return this.$store.state.hospital.hospitalManagerInfo
        },
        hospitalInfo() {
            return this.$store.state.hospital.hospitalInfo
        }
    }
}
</script>

<style scoped>
.title {
    font-size: 1.1rem;
    font-weight: 550;
}

.center {
    text-align: center;
}

.hospital-info {
    display: flex;
    justify-content: space-around;
    align-items: flex-start;
}

.box-card {
    width: 45%;
}
</style>